<?php
/**
 * Created by PhpStorm.
 * User: 陈阳
 * Date: 2021/1/31
 * Time: 9:00
 */
namespace app\admin\model;
use think\model;
class GoodsProp extends Model
{
    public function getShanChuAttr($value){
        if(\think\facade\Config::has('search.if_show')){
            $config = \think\facade\Config::get('search.if_show');
            return $config[$value];
        }
    }

    public function searchPnameAttr($query,$value){
        if(''!==$value){
            $query->where('pname','like','%'.$value.'%');
        }
    }
    public function searchVidAttr($query,$value){
        if(!empty($value)){
          foreach ($value as $v){
              $query->whereFindInSet('pvalues',$v);
          }
        }
    }
    public static function getGoodsPropList($data = [],$query = [], $page = true){
         $model= self::field(true)->withSearch(array_keys($data),$data);
        if($page){
            $list_rows = \think\facade\Config::has('view.list_rows')?\think\facade\Config::get('view.list_rows'):15;
            return $model->paginate([
                'list_rows' => 3,//分页的配置参数
                'query'     => $query,
            ])->each(function($v,$k){
                $shuxing=self::table('ecm_goods_prop_value')
                    ->field('pid,prop_value')->where("vid in ({$v['pvalues']})")->select();
                $arr=[];
                foreach($shuxing as $kk=>$vv){
                    $arr[]=$vv['prop_value'];
                    $a=implode(',',$arr);
                }
                $v['a']=$a;
                return $v;
            });
        }else{
            return $model->select();
        }
    }



    public static function getGoodsPropListt($data = [],$query = [], $page = true){
        $model= self::field(true)->withSearch(array_keys($data),$data);
        if(isset($data['pname']) && '' !== $data['pname']){
            $model->whereLike('pname','%'.$data['pname'].'%');
        }
        if(!empty($data['vid'])){
          foreach ($data['vid'] as $v){
              $model->whereFindInSet('pvalues',$v);
          }
        }
        if($page){
            $list_rows = \think\facade\Config::has('view.list_rows')?\think\facade\Config::get('view.list_rows'):15;
            return $model->paginate([
                'list_rows' => 3,//分页的配置参数
                'query'     => $query,
            ])->each(function($v,$k){
                $shuxing=self::table('ecm_goods_prop_value')
                    ->field('pid,prop_value')->where("vid in ({$v['pvalues']})")->select();
                $arr=[];
                foreach($shuxing as $kk=>$vv){
                    $arr[]=$vv['prop_value'];
                    $a=implode(',',$arr);
                }
                $v['a']=$a;
                return $v;
            });
        }else{
            return $model->select();
        }
    }
}